#include"myredis++.h"
Myredis::Myredis(string connection_info):_redis(Redis(connection_info))
{
    if(pthread_mutex_init(&this->_mutx,nullptr)!=0)
    {
        perror("pthread_mutex_init");
        return;
    }
}
//头插
void Myredis::push(string key,vector<string>values)
{
try
{
pthread_mutex_lock(&this->_mutx);
this->_redis.lpush(key,values.begin(),values.end());
pthread_mutex_unlock(&this->_mutx);
}   
catch(const std::exception&e)
{
pthread_mutex_unlock(&this->_mutx);
perror("push Abnormal");
}
}
//尾插
void Myredis::rpush(string key,vector<string>values)
{
try
{
pthread_mutex_lock(&this->_mutx);
this->_redis.rpush(key,values.begin(),values.end());
pthread_mutex_unlock(&this->_mutx);
}
catch(const std::exception&e)
{
pthread_mutex_unlock(&this->_mutx);
perror("rpush Abnormal");
}

}
//设置过期时间
void Myredis::set_Overdue(string key,int time)
{
try{
pthread_mutex_lock(&this->_mutx);
if(!this->_redis.expire(key,time))
{
    perror("myredis set_overdue");
}
pthread_mutex_unlock(&this->_mutx);
}
catch(const std::exception&e)
{
pthread_mutex_unlock(&this->_mutx);
perror("set_overdue Abnormal");
}
}
//查看过期时间
long long Myredis::get_Overdue(string key)
{
try{
pthread_mutex_lock(&this->_mutx);
long long over_time=this->_redis.ttl(key); 
pthread_mutex_unlock(&this->_mutx);   
return over_time;
}
catch(const std::exception&e)
{
pthread_mutex_unlock(&this->_mutx);
perror("get_Overdue Abnormal");
}
return 0;
}
//查询redis是否有key,有的情况下取出values
bool Myredis::get_list(string key,vector<string>& values)
{
try
{
pthread_mutex_lock(&this->_mutx);
this->_redis.lrange(key,0,-1,std::back_inserter(values));
if(values.size()==0)
{
    return false;
pthread_mutex_unlock(&this->_mutx);   
}
pthread_mutex_unlock(&this->_mutx); 
return true;
}
catch(const std::exception&e)
{
pthread_mutex_unlock(&this->_mutx);
perror("get_list Abnormal");
}
return 0;

}
